home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / brailler-04b-c / brlr ƒ / Shell ƒ / text twiddling.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  7.0 KB  |  331 lines  |  [TEXT/MMCC]

  1. #include "text twiddling.h"
  2. #include "window layer.h"
  3.  
  4. Boolean AnyTextInScrapQQ(void)
  5. {
  6.     long            dummy;
  7.     
  8.     LoadScrap();
  9.     return (GetScrap(0L, 'TEXT', &dummy)!=noTypeErr);
  10. }
  11.  
  12. void SetTheText(WindowPtr theWindow, Ptr data, long count)
  13. {
  14.     TEHandle        hTE;
  15.     
  16.     hTE=GetWindowTE(theWindow);
  17.     if (hTE==0L)
  18.         return;
  19.     
  20.     TESetText(data, count, hTE);
  21.     TESetSelect(0, 0, hTE);
  22.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  23. }
  24.  
  25. Boolean AnyTextQQ(WindowPtr theWindow)
  26. {
  27.     TEHandle        hTE;
  28.     
  29.     hTE=GetWindowTE(theWindow);
  30.     if (hTE==0L)
  31.         return FALSE;
  32.     else
  33.         return ((**hTE).teLength!=0);
  34. }
  35.  
  36. Boolean AnyHighlightedQQ(WindowPtr theWindow)
  37. {
  38.     TEHandle        hTE;
  39.     
  40.     hTE=GetWindowTE(theWindow);
  41.     if (hTE==0L)
  42.         return FALSE;
  43.     else
  44.         return ((**hTE).selStart!=(**hTE).selEnd);
  45. }
  46.  
  47. short SelectionStart(WindowPtr theWindow)
  48. {
  49.     TEHandle        hTE;
  50.     
  51.     hTE=GetWindowTE(theWindow);
  52.     if (hTE==0L)
  53.         return -1;
  54.     
  55.     return (**hTE).selStart;
  56. }
  57.  
  58. short SelectionEnd(WindowPtr theWindow)
  59. {
  60.     TEHandle        hTE;
  61.     
  62.     hTE=GetWindowTE(theWindow);
  63.     if (hTE==0L)
  64.         return -1;
  65.     
  66.     return (**hTE).selEnd;
  67. }
  68.  
  69. Boolean InsertBeforeStart(WindowPtr theWindow, Str255 theStr)
  70. {
  71.     TEHandle        hTE;
  72.     unsigned long    len;
  73.     unsigned long    oldLen;
  74.     
  75.     len=theStr[0];
  76.     hTE=GetWindowTE(theWindow);
  77.     oldLen=(**hTE).teLength;
  78.     if (oldLen+len>32767)
  79.         return FALSE;
  80.     
  81.     TEInsert(&theStr[1], len, hTE);
  82.     return TRUE;
  83. }
  84.  
  85. Boolean InsertAfterEnd(WindowPtr theWindow, Str255 theStr)
  86. {
  87.     TEHandle        hTE;
  88.     unsigned long    len;
  89.     unsigned long    oldLen;
  90.     unsigned long    oldSelStart;
  91.     unsigned long    oldSelEnd;
  92.     
  93.     len=theStr[0];
  94.     hTE=GetWindowTE(theWindow);
  95.     oldLen=(**hTE).teLength;
  96.     if (oldLen+len>32767)
  97.         return FALSE;
  98.     
  99.     oldSelStart=(**hTE).selStart;
  100.     oldSelEnd=(**hTE).selEnd;
  101.     TESetSelect(oldSelEnd, oldSelEnd, hTE);
  102.     TEInsert(&theStr[1], len, hTE);
  103.     TESetSelect(oldSelStart, oldSelEnd, hTE);
  104.     return TRUE;
  105. }
  106.  
  107. short TotalNumberOfLines(TEHandle hTE)
  108. {
  109.     short            numLines;
  110.     
  111.     numLines=(**hTE).nLines;
  112.     if (*((unsigned char*)((long)(*((**hTE).hText))+(**hTE).teLength-1))==0x0d)
  113.         numLines++;
  114.     
  115.     return numLines;
  116. }
  117.  
  118. pascal void ScrollActionProc(ControlHandle theHandle, short partCode)
  119. {
  120.     short            scrollDistance;
  121.     TEHandle        hTE;
  122.     WindowPtr        theWindow;
  123.     
  124.     theWindow=(**theHandle).contrlOwner;
  125.     if (theWindow==0L)
  126.         return;
  127.     
  128.     hTE=GetWindowTE(theWindow);
  129.     if (hTE==0L)
  130.         return;
  131.     
  132.     switch (partCode)
  133.     {
  134.         case inUpButton:
  135.         case inDownButton:
  136.             scrollDistance=(**hTE).lineHeight;
  137.             break;
  138.         case inPageUp:
  139.         case inPageDown:
  140.             scrollDistance=(**hTE).viewRect.bottom-(**hTE).viewRect.top-(**hTE).lineHeight;
  141.             break;
  142.         default:
  143.             scrollDistance=0;
  144.             break;
  145.     }
  146.     
  147.     if ((partCode==inDownButton) || (partCode==inPageDown))
  148.         scrollDistance=-scrollDistance;
  149.     
  150.     MyMoveScrollBox(theHandle, scrollDistance/(**hTE).lineHeight);
  151.     
  152.     if (scrollDistance!=0)
  153.     {
  154.         TEPinScroll(0, scrollDistance, hTE);
  155.     }
  156. }
  157.  
  158. void MyMoveScrollBox(ControlHandle theControl, short scrollDistance)
  159. {
  160.     short            oldSetting, setting, max;
  161.     
  162.     oldSetting=GetControlValue(theControl);
  163.     max=GetControlMaximum(theControl);
  164.     setting=oldSetting-scrollDistance;
  165.     if (setting<0)
  166.         setting=0;
  167.     else if (setting>max)
  168.         setting=max;
  169.     
  170.     SetControlValue(theControl, setting);
  171. }
  172.  
  173. void AdjustVScrollBar(ControlHandle theControl, TEHandle hTE)
  174. {
  175.     short            oldValue, oldMax;
  176.     short            numLines, max, value;
  177.     
  178.     oldMax=GetControlMaximum(theControl);
  179.     oldValue=GetControlValue(theControl);
  180.     numLines=TotalNumberOfLines(hTE);
  181.     max=numLines-(((**hTE).viewRect.bottom-(**hTE).viewRect.top)/((**hTE).lineHeight));
  182.     if (max<0)
  183.         max=0;
  184.     SetControlMaximum(theControl, max);
  185.     value=((**hTE).viewRect.top-(**hTE).destRect.top)/((**hTE).lineHeight);
  186.     if (value<0)
  187.         value=0;
  188.     else if (value>max)
  189.         value=max;
  190.     SetControlValue(theControl, value);
  191. }
  192.  
  193. short CurrentLineNumber(TEHandle hTE)
  194. {
  195.     short            i;
  196.     short            currentEndPoint;
  197.     short            lineNumber;
  198.     short            numLines;
  199.     
  200.     currentEndPoint=(**hTE).selEnd;
  201.     lineNumber=0;
  202.     numLines=(**hTE).nLines;
  203.     for (i=0; i<numLines; i++)
  204.     {
  205.         if ((**hTE).lineStarts[i]<currentEndPoint)
  206.             lineNumber++;
  207.         else
  208.             return lineNumber;
  209.     }
  210.     
  211.     return numLines;
  212. }
  213.  
  214. pascal Boolean MyClikLoop(void)
  215. {
  216.     Point            thePoint;
  217.     TEHandle        hTE;
  218.     WindowPtr        theWindow;
  219.     GrafPtr            curPort;
  220.     
  221.     theWindow=GetFrontDocumentWindow();
  222.     if (theWindow==0L)
  223.         theWindow=FrontWindow();
  224.     if (theWindow==0L)
  225.         return FALSE;
  226.     
  227.     hTE=GetWindowTE(theWindow);
  228.     if (hTE==0L)
  229.         return FALSE;
  230.     
  231.     GetPort(&curPort);
  232.     SetPort(theWindow);
  233.     GetMouse(&thePoint);
  234.     if (!PtInRect(thePoint, &((**hTE).viewRect)))
  235.     {
  236.         if (thePoint.v<((**hTE).viewRect.top))
  237.         {
  238.             if ((**hTE).selStart>0)
  239.                 TEPinScroll(0, (**hTE).lineHeight, hTE);
  240.         }
  241.         else if (thePoint.v>(**hTE).viewRect.bottom)
  242.         {
  243.             if ((**hTE).selEnd<(**hTE).teLength)
  244.                 TEPinScroll(0, -(**hTE).lineHeight, hTE);
  245.         }
  246.         
  247.         AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  248.     }
  249.     
  250.     SetPort(curPort);
  251.     
  252.     return TRUE;
  253. }
  254.  
  255. void AdjustForEndScroll(ControlHandle theControl, TEHandle hTE)
  256. {
  257.     short            numLines;
  258.     short            offset;
  259.     
  260.     numLines=TotalNumberOfLines(hTE);
  261.     offset=numLines-GetControlMaximum(theControl)-
  262.         (((**hTE).viewRect.bottom-(**hTE).viewRect.top)/(**hTE).lineHeight);
  263.     if (offset<0)
  264.         TEPinScroll(0, -offset*((**hTE).lineHeight), hTE);
  265. }
  266.  
  267. #define kTextMargin    2
  268.  
  269. /* Return a rectangle that is inset from the portRect by the size of
  270.     the scrollbars and a little extra margin. */
  271.  
  272. void GetTERect(WindowPtr window, Rect *teRect)
  273. {
  274.     *teRect = window->portRect;
  275.     InsetRect(teRect, kTextMargin, kTextMargin);    /* adjust for margin */
  276.     teRect->bottom = teRect->bottom - 15;        /* and for the scrollbars */
  277.     teRect->right = teRect->right - 15;
  278. }
  279.  
  280.  
  281. /* Update the TERec's view rect so that it is the greatest multiple of
  282.     the lineHeight that still fits in the old viewRect. */
  283.  
  284. void AdjustViewRect(TEHandle docTE)
  285. {
  286.     TEPtr        te;
  287.     
  288.     te = *docTE;
  289.     te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
  290.                             * te->lineHeight) + te->viewRect.top;
  291. }
  292.  
  293.  
  294. /*    Re-calculate the position and size of the viewRect and the scrollbars.
  295.     kScrollTweek compensates for off-by-one requirements of the scrollbars
  296.     to have borders coincide with the growbox. */
  297.  
  298. #define kScrollbarWidth        16
  299. #define kScrollbarAdjust    (kScrollbarWidth-1)
  300. #define kScrollTweek        2
  301.  
  302. void AdjustScrollSizes(WindowPtr window, TEHandle hTE, ControlHandle vScrollBar,
  303.     ControlHandle hScrollBar)
  304. {
  305.     Rect        teRect;
  306.     
  307.     GetTERect(window, &teRect);                            /* start with TERect */
  308.     (**hTE).viewRect = teRect;
  309.     (**hTE).destRect.left=(**hTE).viewRect.left;
  310.     (**hTE).destRect.right=(**hTE).viewRect.right;
  311.     MoveControl(vScrollBar, window->portRect.right - kScrollbarAdjust, -1);
  312.     SizeControl(vScrollBar, kScrollbarWidth, (window->portRect.bottom - 
  313.                 window->portRect.top) - (kScrollbarAdjust - kScrollTweek));
  314.     MoveControl(hScrollBar, -1, window->portRect.bottom - kScrollbarAdjust);
  315.     SizeControl(hScrollBar, (window->portRect.right - 
  316.                 window->portRect.left) - (kScrollbarAdjust - kScrollTweek),
  317.                 kScrollbarWidth);
  318. }
  319.  
  320. void AdjustTE(TEHandle hTE, ControlHandle vScrollBar, ControlHandle hScrollBar)
  321. {
  322.     TEPtr        te;
  323.     
  324.     te = *hTE;
  325.     TEScroll((te->viewRect.left - te->destRect.left) -
  326.             GetControlValue(hScrollBar),
  327.             ((te->viewRect.top - te->destRect.top)/te->lineHeight) -
  328.                 GetControlValue(vScrollBar),
  329.             hTE);
  330. }
  331.